#!/bin/bash

# 编译的CPU核心数
coreNum=1
if [ $# -ne 0 ]; then
  coreNum=$1
fi

BUILD_DIR=/opt

cd $BUILD_DIR
git clone --depth=1 https://gitclone.com/github.com/gperftools/gperftools.git
git clone --depth=1 https://github.com/gabime/spdlog.git
git clone --depth=1 https://github.com/cameron314/readerwriterqueue.git
git clone --depth=1 https://github.com/cameron314/concurrentqueue.git
git clone --depth=1 https://github.com/take-cheeze/mruby-picojson.git
git clone https://github.com/zeromq/libzmq.git
git clone https://github.com/zeromq/zmqpp.git
git clone https://github.com/include-what-you-use/include-what-you-use.git
git clone https://github.com/sewenew/redis-plus-plus.git





cp -r /opt/apache-zookeeper-3.8.4-bin/conf/zoo_sample.cfg  /opt/apache-zookeeper-3.8.4-bin/conf/zoo.cfg
sed -i 's/dataDir=\/tmp\/zookeeper/dataDir=\/opt\/zookeeperData/g' /opt/apache-zookeeper-3.8.4-bin/conf/zoo.cfg

cd $BUILD_DIR/gperftools && ./autogen.sh && ./configure --prefix=/usr && make && make install && ldocnfig && make clean 
cd $BUILD_DIR/libzmq && git checkout v4.3.5 && git switch -c release-v4.3.5 && cmake -S . -B build -DCMAKE_INSTALL_PREFIX=/usr/ -DCMAKE_BUILD_TYPE=Debug -DBUILD_DRAFT_API=ON && cmake --build build -j $coreNum && cmake --install build && rm -rf build
cd $BUILD_DIR/zmqpp && git checkout 4.2.0 && git switch -c release-4.2.0 && cmake -S . -B build -DCMAKE_INSTALL_PREFIX=/usr/ -DCMAKE_BUILD_TYPE=Debug  && cmake --build build -j $coreNum && cmake --install build && rm -rf build
cd $BUILD_DIR/include-what-you-use && git checkout clang_14 && git switch -c release-clang_14 && cmake -S . -B build -DCMAKE_INSTALL_PREFIX=/usr/ -G "Unix Makefiles" -DIWYU_LLVM_ROOT_PATH=/usr/lib/llvm-14/ && cmake --build build -j $coreNum && cmake --install build && rm -rf build
cd $BUILD_DIR/redis-plus-plus && cmake -S . -B build -DCMAKE_INSTALL_PREFIX=/usr/ -DCMAKE_BUILD_TYPE=Debug  && cmake --build build -j $coreNum && cmake --install build && rm -rf build 

cp -r $BUILD_DIR/spdlog/ /usr/include
cp -r $BUILD_DIR/concurrentqueue/concurrentqueue.h /usr/include/
cp -r $BUILD_DIR/readerwriterqueue/atomicops.h /usr/include/
cp -r $BUILD_DIR/readerwriterqueue/readerwriterqueue.h /usr/include/
cp -r $BUILD_DIR/mruby-picojson/picojson.h /usr/include/